package com.approval.approvalapp.index.cygn.hys;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.Image;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.HysListBean;
import com.approval.approvalapp.center.widget.CommonDialogHelper;
import com.approval.approvalapp.util.DateUtils;

import java.security.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by louyulin on 2019/5/11.
 */

public class HysListAdapter extends RecyclerView.Adapter<
        HysListAdapter.Holder> {

    List<HysListBean> hysListBeanList;

    Context context;

    public void setHysListBeanList(List<HysListBean> hysListBeanList) {
        this.hysListBeanList = hysListBeanList;
        notifyDataSetChanged();
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_hys, parent, false);
        Holder holder = new Holder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(Holder holder, final int position) {
        holder.name.setText(hysListBeanList.get(position).getTitle());
        holder.contant.setText(hysListBeanList.get(position).getContent());
        holder.rv.setLayoutManager(new GridLayoutManager(context, 3));
        Adapter adapter = new Adapter();
        adapter.setListBeanXES(hysListBeanList.get(position).getList());
        adapter.setTitle(hysListBeanList.get(position).getTitle());
        adapter.setId(hysListBeanList.get(position)
                .getId() + "");
        holder.rv.setAdapter(adapter);

        holder.yd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, HysShenqingActivity.class);
                intent.putExtra("id", hysListBeanList.get(position)
                        .getId() + "");
                intent.putExtra("imPlaceN", hysListBeanList.get(position)
                        .getTitle());

                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (hysListBeanList == null) {
            return 0;
        } else {
            return hysListBeanList.size();
        }
    }

    class Holder extends RecyclerView.ViewHolder {
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.yd)
        TextView yd;
        @Bind(R.id.contant)
        TextView contant;
        @Bind(R.id.rv)
        RecyclerView rv;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class Adapter extends RecyclerView.Adapter<Adapter.Holder> {
        List<HysListBean.ListBean> listBeanXES;
        String title;
        String id;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public void setListBeanXES(List<HysListBean.ListBean> listBeanXES) {
            this.listBeanXES = listBeanXES;
            notifyDataSetChanged();
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_item_hys, parent, false);
            Holder holder = new Holder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(Holder holder, int position) {
//            imStart 审核状态0待审核1通过 为空没预约
            final HysListBean.ListBean listBean = listBeanXES.get(position);
            String type = "";
            if (TextUtils.isEmpty(listBean.getImStart())) {
                //未预约
                type = "未预约";
                String d_data = listBean.getD_Data();
                holder.tv1.setText(d_data);
                holder.tv2.setText(listBean.getZj());
                holder.tv3.setText("未预约");
                holder.tv3.setTextColor(Color.parseColor("#7D8198"));
                holder.tv1.setTextColor(Color.parseColor("#333333"));
                holder.tv2.setTextColor(Color.parseColor("#333333"));
                holder.root.setBackgroundResource(R.drawable.fafafashape4);
                holder.ivType.setVisibility(View.GONE);
            } else if (listBean.getImStart().equals("1")) {
                //通过
                type = "通过";
                holder.root.setBackgroundResource(R.drawable.redbordershape4);
                String d_data = listBean.getD_Data();
                holder.tv1.setText(d_data);
                holder.tv2.setText(listBean.getZj());
                String imStartTime = listBean.getImStartTime();
                String imEndTime = listBean.getImEndTime();
                String time = getTime(Long.valueOf(imStartTime)*1000) + " - " + getTime(Long.valueOf(imEndTime)*1000);
                holder.tv3.setText(time);
                holder.ivType.setVisibility(View.VISIBLE);
                holder.ivType.setImageResource(R.drawable.meetingmore_red);
                holder.tv3.setTextColor(Color.parseColor("#DE463A"));
                holder.tv1.setTextColor(Color.parseColor("#DE463A"));
                holder.tv2.setTextColor(Color.parseColor("#DE463A"));
            } else if (listBean.getImStart().equals("0")) {
                type = "审批中";
                holder.tv1.setTextColor(Color.parseColor("#333333"));
                holder.tv2.setTextColor(Color.parseColor("#333333"));
                holder.tv3.setTextColor(Color.parseColor("#1465FD"));
                holder.ivType.setVisibility(View.VISIBLE);
                holder.ivType.setImageResource(R.drawable.meetingmore_blue);
                holder.root.setBackgroundResource(R.drawable.blueborder4);
                String d_data = listBean.getD_Data();
                holder.tv1.setText(d_data);
                holder.tv2.setText(listBean.getZj());
                String imStartTime = listBean.getImStartTime();
                String imEndTime = listBean.getImEndTime();
                String time = getTime(Long.valueOf(imStartTime)*1000) + " - " + getTime(Long.valueOf(imEndTime)*1000);
                holder.tv3.setText(time);

            }

            final String finalType = type;
            holder.root.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (!finalType.equals("未预约")){
                    CommonDialogHelper.getInstance().getCommonDialog(context,getTitle(),getId() ,finalType,listBean);
                    }
                }
            });

        }

        private String getTime(long times) {
            Date date = new Date(times);
            SimpleDateFormat format = new SimpleDateFormat("HH:mm");
            return format.format(date);
        }

        @Override
        public int getItemCount() {
            if (listBeanXES == null) {
                return 0;
            } else {
                return listBeanXES.size();
            }
        }

        class Holder extends RecyclerView.ViewHolder {
            @Bind(R.id.tv1)
            TextView tv1;
            @Bind(R.id.tv2)
            TextView tv2;
            @Bind(R.id.tv3)
            TextView tv3;
            @Bind(R.id.tv4)
            TextView tv4;
            @Bind(R.id.iv_type)
            ImageView ivType;

            @Bind(R.id.root)
            LinearLayout root;

            public Holder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }

}
